/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "数据工具库.h"
#include "字符串数值互换.h"


#include <字符串转换.h>


// This is an example of an exported variable
int n数据工具库=0;

// This is an example of an exported function.
int fn数据工具库(void) {
	return 42;
}

// This is the constructor of a class that has been exported.
// see 数据工具库.h for the class definition
C数据工具库::C数据工具库() {
	return;
}

C_Key分配器::C_Key分配器() {
	m_末尾 = 0;
}

uint32 C_Key分配器::f_Gen() {
	uint32 key;
	/* = m_Key.size();
	
	if (m_Key.find(key) != m_Key.end()) {
		m_Key.insert(key);
	}
	else {
		key = m_回收站.back();
		m_回收站.pop_back();
	}*/

	if (m_回收站.size()) {
		key = m_回收站.back();
		m_回收站.pop_back();
	}
	else {
		key = m_末尾;
		++m_末尾;
	}

	return key;
}

void C_Key分配器::f_Move(uint32 key) {
	//m_Key.erase(key);
	m_回收站.push_back(key);
}

void C_Key分配器::f_Clear() {
	m_回收站.clear();
	m_末尾 = 0;
}



C_StrKey分配器::C_StrKey分配器() {
	m_末尾 = 0;
}

std::u16string C_StrKey分配器::f_Gen(std::u16string 前缀) {
	if(m_Key.find(前缀) == m_Key.end()) {
		m_Key.insert(前缀);
		return 前缀;
	}

	if (m_回收站.empty()) {
		while (前缀.size()) {
			auto t = 前缀.back();
			if ( (t >= '0' && t <= '9') || t == '.') {
				前缀.pop_back();
			}
			else {
				break;
			}
		}

		std::u16string newName = 前缀 + u'.' + f_整数值转宽字符串(m_末尾);

		if (m_Key.find(newName) != m_Key.end()) {
			//while (newName.size()) {
			//	auto t = newName.back();
			//	if (t >= '0' && t <= '9') {
			//		newName.pop_back();
			//	}
			//	else {
			//		break;
			//	}
			//}
			//前缀 = newName;

			for (uint32 i = 0; i < -2; ++i) {
				++m_末尾;
				newName = 前缀 + u'.' + f_整数值转宽字符串(m_末尾);

				if (m_Key.find(newName) == m_Key.end()) {
					m_Key.insert(newName);

					return newName;
				}
			}
		}
		else {
			前缀 = newName;
		}
		
	}
	else {
		前缀 = *m_回收站.begin();
		m_回收站.erase(m_回收站.begin());
	}

	m_Key.insert(前缀);
	return 前缀;
}

//void C_StrKey分配器::f_分配名称(std::u16string& name) {
//	name = f_Gen(name);
//}

void C_StrKey分配器::f_Move(std::u16string key) {
	//m_Key.insert(key);
	m_Key.erase(key);
}

bool C_StrKey分配器::f_Add(std::u16string key) {
	if (m_Key.find(key) != m_Key.end()) {
		m_Key.insert(key);
	}
	return false;
}

void C_StrKey分配器::f_Clear() {
	m_Key.clear();
	m_末尾 = 0;
}

//DEF_WString C_StrKey分配器::f_Gen(DEF_WString key) {
//	auto tempName = f_str_u16_to_wstring_v2(key);
//	tempName = f_Gen(tempName);
//	return f_str_wstring_to_u16(tempName);
//}
//
//void C_StrKey分配器::f_move(const DEF_WString& key) {
//	auto tempName = f_str_u16_to_wstring_v2(key);
//	f_Move(tempName);
//}

bool C_StrKey分配器::f_已存在(DEF_WString key) {
	if (m_Key.find(key) != m_Key.end()) {
		return true;
	}
	return false;
}



